import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getwidget/components/image/gf_image_overlay.dart';
import 'package:health/utils/colors.dart';
import 'package:health/utils/styles.dart';
import 'package:health/utils/tools.dart';

class DiscoverController extends GetxController with GetSingleTickerProviderStateMixin {
  late TabController tabController;

  RxList<String> imgs = RxList();

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: 2, vsync: this);
    imgs.addAll([
      'https://img2.baidu.com/it/u=3853345508,384760633&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1200',
      'https://img1.baidu.com/it/u=3065838285,2676115581&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=333',
      'https://img1.baidu.com/it/u=1310564963,1641173348&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800'
    ]);
  }

  @override
  void onClose() {
    super.onClose();
    tabController.dispose();
  }
}

class DiscoverPage extends GetView<DiscoverController> {
  final c = Get.put(DiscoverController());

  DiscoverPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DiscoverController>(
      builder: (_) {
        return Scaffold(
          backgroundColor: ColorsUtils.rgba(242, 242, 242, 1),
          body: CustomScrollView(
            slivers: [
              SliverAppBar(
                pinned: true,
                elevation: 0,
                expandedHeight: 200,
                automaticallyImplyLeading: false,
                backgroundColor: Colors.white,
                flexibleSpace: FlexibleSpaceBar(
                    title: Text('广告位',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color0000, fontSize: 18)),
                    centerTitle: true,
                    background: Obx(() => Swiper.children(
                          itemHeight: 200,
                          containerHeight: 200,
                          autoplay: true,
                          children: controller.imgs
                              .map((element) => GFImageOverlay(
                                    height: 200,
                                    alignment: Alignment.topLeft,
                                    image: NetworkImage(element),
                                  ))
                              .toList(),
                        ))),
              ),
              SliverPersistentHeader(
                pinned: true,
                delegate: StickyTabBarDelegate(
                  child: TabBar(
                    indicatorColor: Colors.blueAccent,
                    controller: controller.tabController,
                    indicatorWeight: 4,
                    indicatorSize: TabBarIndicatorSize.label,
                    labelStyle:
                        StylesUtils.customTextStyle(color: ColorsUtils.color096d, fontSize: 16),
                    unselectedLabelStyle:
                        StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 16),
                    tabs: [
                      Tab(
                        text: '活动',
                        icon: ToolsUtils.getUrlSvg(
                            'https://img.axureshop.com/d1/0d/00/d10d002b3fd44ba5aacc6b974316320e/images/发现/u3742.svg',
                            width: 25,
                            height: 25),
                      ),
                      Tab(
                        text: '资讯',
                        icon: ToolsUtils.getUrlSvg(
                            'https://img.axureshop.com/d1/0d/00/d10d002b3fd44ba5aacc6b974316320e/images/发现/u3742.svg',
                            width: 25,
                            height: 25),
                      ),
                    ],
                  ),
                ),
              ),
              SliverFillRemaining(
                child: TabBarView(
                  controller: controller.tabController,
                  children: const [
                    Center(child: Text('Content of Home')),
                    Center(child: Text('Content of Profile')),
                  ],
                ),
              ),
            ],
          ),
        );
      },
      init: controller,
    );
  }
}

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar child;

  StickyTabBarDelegate({required this.child});

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return child;
  }

  @override
  double get maxExtent => child.preferredSize.height;

  @override
  double get minExtent => child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
